/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BL;

import Bean.NguoiDung;
import Bean.PhanQuyen;
import Util.DBConnection;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author AD_DA
 */
public class BLNguoiDung {
    public NguoiDung getTenSuDung(String TenSuDung) throws SQLException
    {
        Connection conn=null;
        conn=DBConnection.getConnection(); 
        Statement stm=(Statement) conn.createStatement();
        String sql="SELECT * FROM nguoidung WHERE TENSUDUNG='"+TenSuDung+"';";
        ResultSet rs=stm.executeQuery(sql);
        NguoiDung NgDung=new NguoiDung();
        if(rs.next())
        {
            NgDung.setMaNguoiSuDung(rs.getInt("MANGUOIDUNG"));
            NgDung.setTenSuDung(rs.getString("TENSUDUNG"));
            NgDung.setCMND(rs.getString("SOCMND"));
            NgDung.setDiaChi(rs.getString("DIACHI"));
            NgDung.setEmail(rs.getString("EMAIL"));
            //NgDung.setHinhDaiDien(TenSuDung);
            NgDung.setHo(rs.getString("HO"));
            NgDung.setTen(rs.getString("TEN"));
            NgDung.setMatKhau(rs.getString("MATKHAU"));
            NgDung.setSoDT(rs.getString("SODT"));
        }
        conn.close();
        return NgDung;      
    }
    public List<NguoiDung> getLisstNguoiDung() throws SQLException
    {
        List<NguoiDung> lstNgDung= new ArrayList<NguoiDung>();
        Connection conn=null;
        conn=DBConnection.getConnection(); 
        Statement stm=(Statement) conn.createStatement();
        String sql="SELECT * FROM nguoidung";
        ResultSet rs=stm.executeQuery(sql);
        while(rs.next())
        {
            NguoiDung NgDung=new NguoiDung();
            NgDung.setMaNguoiSuDung(rs.getInt("MANGUOIDUNG"));
            NgDung.setTenSuDung(rs.getString("TENSUDUNG"));
            NgDung.setCMND(rs.getString("SOCMND"));
            NgDung.setDiaChi(rs.getString("DIACHI"));
            NgDung.setEmail(rs.getString("EMAIL"));
            //NgDung.setHinhDaiDien(TenSuDung);
            NgDung.setHo(rs.getString("HO"));
            NgDung.setTen(rs.getString("TEN"));
            NgDung.setMatKhau(rs.getString("MATKHAU"));
            NgDung.setSoDT(rs.getString("SODT"));
            lstNgDung.add(NgDung);
        }
        conn.close();
        return lstNgDung;
    }
    public boolean KTTenSuDung(String TenSuDung) throws SQLException
    {
        NguoiDung NgDung=getTenSuDung(TenSuDung);
        if(NgDung.getTenSuDung()!=null)
            return true;
        return false;
    }
    public boolean KTPass(String TenSuDung, String MatKhau) throws SQLException
    {
        NguoiDung NgDung=getTenSuDung(TenSuDung);
        if(NgDung.getTenSuDung()!=null)
            {
            if(NgDung.getMatKhau().equals(MatKhau))
            {
                return true;
            }
        }
        return false;
    }
    public int getMaNguoiDung(String TenSuDung) throws SQLException
    {
        NguoiDung NgDung=getTenSuDung(TenSuDung);
        int id=NgDung.getMaNguoiSuDung();
        return id;
    }
    public void InsertNguoiDung(int MaNguoiDung,String TenSuDung, String MatKhau,String Ho, String Ten, 
                                String SoCMND, String DiaChi, String Email, String SoDT,String HinhDaiDien)
    {
        try{
            Connection conn=null;
            conn=DBConnection.getConnection();
            String sql="INSERT INTO nguoidung(MANGUOIDUNG, TENSUDUNG, MATKHAU, HO, TEN, SOCMND, DIACHI, EMAIL, SODT, HINHDAIDIEN) VALUES (?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement preStm=(PreparedStatement) conn.prepareStatement(sql);
            preStm.setInt(1,MaNguoiDung );
            preStm.setString(2,TenSuDung );
            preStm.setString(3,MatKhau );
            preStm.setString(4,Ho );
            preStm.setString(5,Ten );
            preStm.setString(6,SoCMND );
            preStm.setString(7,DiaChi );
            preStm.setString(8,Email );
            preStm.setString(9,SoDT );
            preStm.setString(10,HinhDaiDien );
            int rs=preStm.executeUpdate();
            conn.close();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
    }
    public void UpdateNguoiDung(int MaNguoiDung,String TenSuDung, String MatKhau,String Ho, String Ten, 
                                String SoCMND, String DiaChi, String Email, String SoDT,String HinhDaiDien)
    {
        try{
            Connection conn=null;
            conn=DBConnection.getConnection();
            String sql="UPDATE nguoidung SET TENSUDUNG=?,MATKHAU=?,HO=?,TEN=?,SOCMND=?,DIACHI=?,EMAIL=?,SODT=?,HINHDAIDIEN=? WHERE MANGUOIDUNG=? ;";
            PreparedStatement preStm=(PreparedStatement) conn.prepareStatement(sql);            
            preStm.setString(1,TenSuDung );
            preStm.setString(2,MatKhau );
            preStm.setString(3,Ho );
            preStm.setString(4,Ten );
            preStm.setString(5,SoCMND );
            preStm.setString(6,DiaChi );
            preStm.setString(7,Email );
            preStm.setString(8,SoDT );
            preStm.setString(9,HinhDaiDien );
            preStm.setInt(10,MaNguoiDung );
            int rs=preStm.executeUpdate();
            conn.close();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    public void DeleteNguoiDung(int MaNguoiDung)
    {
        try{
            Connection conn=null;
            conn=DBConnection.getConnection(); 
            Statement stm=(Statement) conn.createStatement();
            String sql="DELETE FROM nguoidung WHERE MANGUOIDUNG="+MaNguoiDung+";";
            int rs=stm.executeUpdate(sql);
            conn.close();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    public PhanQuyen getQuyenSD(int MaNguoiDung) throws SQLException{
        Connection conn=null;
        conn=DBConnection.getConnection();
        
        if(conn!=null){
            String sql="SELECT MAQSD, MANGUOIDUNG FROM phanquyen WHERE MANGUOIDUNG="+MaNguoiDung;
            Statement stm=(Statement) conn.createStatement();
            ResultSet rs= stm.executeQuery(sql);
            if(rs.next()){
                PhanQuyen pq=new PhanQuyen();
                pq.setMaQuyen(rs.getInt("MAQSD"));
                pq.setMaNguoiDung(rs.getInt("MANGUOIDUNG"));
                return pq;
            }
        }
        return null;
    }
    
}
